﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlayPrima
{



    public class SvtPrima
    {
        string str;
        List<SPCategory> categories = new List<SPCategory>();
        public List<SPCategory> Categories
        {
            get { return categories; }
            set { categories = value; }
        }
        public void Load()
        {
            string listing = "http://cms009.dc2.movenetworks.com/cms/publish/vod2/vodchannel/svtinteraktiv/4/listing.json";

            string urll = "http://playprima.se/";
            //webBrowser1.Navigate("about:blank");            


            System.Net.WebClient wc = new System.Net.WebClient();
            str = wc.DownloadString(listing);
            str = str.Substring(1);
            str = str.Remove(str.Length - 1);

            //textBox2.Text = str;

            Structure st = new Structure();
            st.Parse(str);
            foreach (Structure ser in ((List<object>)st.Members["categoryList"]))
            {
                int sid = int.Parse(ser.Members["id"].ToString());

                SPCategory spc = new SPCategory();
                spc.Title = ser.Members["name"].ToString();
                spc.id = sid;
                spc.ImageUrl = ((Structure)ser.Members["thumbnail"]).Members["thumb_url"].ToString();

                //foreach (KeyValuePair<string, object> p in ser.Members)
                //{
                //    textBox2.Text += p.Key + " : " + p.Value.ToString() + Environment.NewLine;

                //}
                foreach (Structure clip in ((List<object>)ser.Members["clipList"]))
                {
                    int cid = int.Parse(clip.Members["id"].ToString());
                    //  textBox2.Text += "  " + clip.Members["name"] + "  http://playprima.se/#|"+sid.ToString()+"|"+cid.ToString()+"|" + Environment.NewLine;

                    SPClip cli = new SPClip();
                    cli.Title = clip.Members["name"].ToString();
                    cli.Desc = clip.Members["description"].ToString();
                    cli.id = cid;
                    spc.Clip.Add(cli);

                    //"http://playprima.se/#|34|369|"
                }
                categories.Add(spc);
                //textBox2.Text += Environment.NewLine;
            }
        }
    }



    public class SPClip
    {
        public string Title;

        public string Desc;
        public int id;
        public override string ToString()
        {
            return Title;
        }
    }
    public class SPCategory
    {
        public string ImageUrl;
        public string Title = "";
        public int id;
        public List<SPClip> Clip = new List<SPClip>();
        public override string ToString()
        {
            return Title;
        }
    }


    public class Structure
    {
        public Dictionary<string, object> Members = new Dictionary<string, object>();
        public override string ToString()
        {
            string str = "";
            foreach (KeyValuePair<string, object> p in Members)
            {
                str += p.Key + " : " + p.Value.ToString() + ", ";


            }
            return "{ " + str + " }";
        }
        public void Parse(string str)
        {
            bool quote = false;
            string name = "";
            string value = "";
            bool stru = false;
            bool islist = false;
            List<object> list = new List<object>();
            int lvl = 0;
            string buffer = "";
            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];

                if (c == '\"')
                {
                    quote = !quote;
                    buffer += c;
                }
                else if (!quote)
                {
                    if (lvl == 0)
                    {
                        if (c == '{')
                        {
                            lvl++;
                            stru = true;
                        }
                        else if (c == '[')
                        {
                            islist = true;
                        }
                        else if (c == ']')
                        {
                            if (stru)//if (list.Count>0)
                            {
                                if (buffer.Trim() == "" || name.Trim() == "") continue;
                                object obj;
                                value = buffer;
                                if (stru)
                                {
                                    Structure stu = new Structure();
                                    stu.Parse(value);
                                    obj = stu;
                                    stru = false;
                                }
                                else obj = value.Trim().Replace("\"", "");

                                list.Add(obj);
                            }
                            buffer = "";
                            islist = false;
                            Members.Add(name.Trim().Replace("\"", ""), list);
                            list = new List<object>();
                            buffer = "";
                            name = "";
                            stru = false;
                        }
                        else if (c == ',')
                        {
                            if (buffer.Trim() == "" || name.Trim() == "") continue;
                            object obj;
                            value = buffer;
                            if (stru)
                            {
                                Structure stu = new Structure();
                                stu.Parse(value);
                                obj = stu;
                                stru = false;
                            }
                            else obj = value.Trim().Replace("\"", "");
                            if (islist)
                            {
                                list.Add(obj);
                            }
                            else
                            {
                                Members.Add(name.Trim().Replace("\"", ""), obj);
                            }
                            buffer = "";
                        }
                        else if (c == ':')
                        {
                            name = buffer;
                            buffer = "";
                        }
                        else buffer += c;
                    }
                    else if (c == '{')
                    {
                        lvl++;
                        buffer += c;
                    }
                    else if (c == '}')
                    {
                        lvl--;
                        if (lvl > 0)
                        {
                            buffer += c;
                        }
                    }
                    else buffer += c;
                }
                else
                {
                    buffer += c;
                }
            }
            if (buffer.Trim() != "")
            {
                object obj;
                value = buffer;
                if (stru)
                {
                    Structure stu = new Structure();
                    stu.Parse(value);
                    obj = stu;
                    stru = false;
                }
                else obj = value.Trim().Replace("\"", "");
                if (islist)
                {
                    list.Add(obj);
                }
                else
                {
                    Members.Add(name.Trim().Replace("\"", ""), obj);
                }
                buffer = "";
            }
        }

    }
}


